﻿$(document).ready(function() {
    GridDemo.Home.Categories.setupGrid($("#grid"), $("#pager"), $("#search"));
});

GridDemo.Home.Categories = {
    setupGrid: function(grid, pager, search) {
        var lastsel;
        grid.jqGrid({
            url: "Categories/List",
            mtype: "get",
            datatype: "json",
            colNames: ['Edit', 'Details', 'CategoryID', 'CategoryName', 'Description'],
            colModel: [
                        { name: 'Edit', index: 'Edit', width: 25, align: 'center', sortable: false, hidden: false },
                        { name: 'Details', index: 'Details', width: 25, align: 'center', sortable: false, hidden: false },
                        { name: 'CategoryID', index: 'CategoryID', width: 50, align: 'center', sortable: false, hidden: false },
                        { name: 'CategoryName', index: 'CategoryName', align: 'left', sortable: true, editable: true, hidden: false },
                        { name: 'Description', index: 'Description', align: 'left', sortable: true, editable: true, hidden: false },
                      ],
            rowNum: 10,
            rowList: [10, 20, 50],
            pager: pager,
            sortname: 'CategoryID',
            sortorder: "asc",
            viewrecords: true,
            multiselect: false,
            onSelectRow: function(id) {
                if (id && id !== lastsel) {
                    //grid.restoreRow(lastsel);
                    grid.saveRow(lastsel, checksave);
                    grid.editRow(id, true);
                    lastsel = id;
                }
            },
            editurl: "Categories/Edit",
            width: '100%',
            height: '100%',
            autowidth: true,
            rownumbers: true,
            caption: 'Categories (Master)',
            onSelectRow: function(ids) {
                if (ids == null) {
                    ids = 0;
                    if (jQuery("#grid_d").getGridParam('records') > 0) {
                        jQuery("#grid_d").setGridParam({ url: "Products/List?categoryId=" + ids, page: 1 }).setCaption("Products (Detail): " + ids).trigger('reloadGrid');
                    }
                } else {
                    jQuery("#grid_d").setGridParam({ url: "Products/List?categoryId=" + ids, page: 1 }).setCaption("Products (Detail): " + ids).trigger('reloadGrid');
                }
            }
        }).navGrid(pager, { edit: false, add: false, del: false, search: false });
        search.filterGrid("#" + grid.attr("id"), {
            gridModel: false,
            filterModel: [{
                label: 'Search',
                name: 'search',
                stype: 'text'
}]
            });
        }
    };

    $(document).ready(function() {
        GridDemo.Home.Products.setupGrid($("#grid_d"), $("#pager_d"), $("#search_d"));
    });

    GridDemo.Home.Products = {
        setupGrid: function(grid, pager, search) {
            var lastsel;
            grid.jqGrid({
                url: "Products/List",
                mtype: "get",
                datatype: "json",
                colNames: ['Edit', 'Details', 'ProductID', 'ProductName', 'UnitPrice', 'Discontinued'],
                colModel: [
                        { name: 'Edit', index: 'Edit', width: 25, align: 'center', sortable: false, hidden: false },
                        { name: 'Details', index: 'Details', width: 25, align: 'center', sortable: false, hidden: false },
                        { name: 'ProductID', index: 'ProductID', width: 50, align: 'center', sortable: false, hidden: false },
                        { name: 'ProductName', index: 'ProductName', sortable: true, editable: true, hidden: false },
                        { name: 'UnitPrice', index: 'UnitPrice', width: 40, align: 'center', formatter: 'currency', formatoptions: { prefix: "$" }, sortable: true, editable: true, hidden: false },
                        { name: 'Discontinued', index: 'Discontinued', width: 50, align: 'center', sortable: false, hidden: false }
                      ],
                rowNum: 10,
                rowList: [10, 20, 50],
                pager: pager,
                sortname: 'ProductID',
                sortorder: "asc",
                viewrecords: true,
                multiselect: false,
                onSelectRow: function(id) {
                    if (id && id !== lastsel) {
                        //grid.restoreRow(lastsel);
                        grid.saveRow(lastsel, checksave);
                        grid.editRow(id, true);
                        lastsel = id;
                    }
                },
                editurl: "Products/Edit",
                width: '100%',
                height: '100%',
                autowidth: true,
                rownumbers: true,
                caption: 'Products (Details)'
            }).navGrid(pager, { edit: false, add: false, del: false, search: false });
            search.filterGrid("#" + grid.attr("id"), {
                gridModel: false,
                filterModel: [{
                    label: 'Search',
                    name: 'search_d',
                    stype: 'text'
}]
                });
            }
        };


        var timeoutHnd;
        var flAuto = false;
        function doSearch(ev) {
            if (ev.keyCode == 13) {
                timeoutHnd = setTimeout(gridReload, 500);
                return;
            }
            if (!flAuto) return;
            // var elem = ev.target||ev.srcElement;
            if (timeoutHnd) {
                clearTimeout(timeoutHnd)
            }
            if ((ev.keyCode >= 65 && ev.keyCode <= 90) || ev.keyCode == 8 || ev.keyCode == 46 || (ev.keyCode >= 48 && ev.keyCode <= 57) || (ev.keyCode >= 96 && ev.keyCode <= 105)) {
                timeoutHnd = setTimeout(gridReload, 500)
            }
        }
        function gridReload() {
            var search = $('#item').val();
            jQuery("#grid").setGridParam({ url: "Categories/List?search=" + search, page: 1 }).trigger("reloadGrid");
        }
        function enableAutosubmit(state) {
            flAuto = state;
            jQuery("#submitButton").attr("disabled", state);
        }

        function checksave(result) {
            if (result == "") {
                alert("Update is missing!");
                return false;
            }
            return true;
        } 